package com.ruoyi.dangerControl.service.impl;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.dangerControl.domain.HdCheckPlan;
import com.ruoyi.dangerControl.dto.LocationDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dangerControl.mapper.LocationMapper;
import com.ruoyi.dangerControl.domain.Location;
import com.ruoyi.dangerControl.service.ILocationService;


/**
 * 地理位置Service业务层处理
 *
 * @author lth
 * @date 2023-12-14
 */
@Service
public class LocationServiceImpl implements ILocationService
{
    @Autowired
    private LocationMapper locationMapper;

    /**
     * 查询地理位置
     *
     * @param locationId 地理位置主键
     * @return 地理位置
     */
    @Override
    public Location selectLocationByLocationId(String locationId)
    {
        return locationMapper.selectLocationByLocationId(locationId);
    }

    /**
     * 查询地理位置列表
     *
     * @param location 地理位置
     * @return 地理位置
     */
    @Override
    public List<Location> selectLocationList(Location location)
    {
        return locationMapper.selectLocationList(location);
    }

    /**
     * 新增地理位置
     *
     * @param location 地理位置
     * @return 结果
     */
    @Override
    public int insertLocation(Location location)
    {
        return locationMapper.insertLocation(location);
    }

    /**
     * 修改地理位置
     *
     * @param location 地理位置
     * @return 结果
     */
    @Override
    public int updateLocation(Location location)
    {
        return locationMapper.updateLocation(location);
    }

    /**
     * 批量删除地理位置
     *
     * @param locationIds 需要删除的地理位置主键
     * @return 结果
     */
    @Override
    public int deleteLocationByLocationIds(String[] locationIds)
    {
        return locationMapper.deleteLocationByLocationIds(locationIds);
    }

    /**
     * 删除地理位置信息
     *
     * @param locationId 地理位置主键
     * @return 结果
     */
    @Override
    public int deleteLocationByLocationId(String locationId)
    {
        return locationMapper.deleteLocationByLocationId(locationId);
    }



    /**
     * 查询位置树列表
     */
    @Override
    public LocationDto getLocationList() {
        //位置列表
        List<Location> locationList = locationMapper.getLocationList();
        List<String> idList = locationList.stream().map(Location::getLocationId).collect(Collectors.toList());

        //荣康煤矿
        Location parentLocation = locationList.get(0);
        //荣康煤矿ID
        String locationId = parentLocation.getLocationId();

        List<Location> childrenList = locationMapper.getChildren(locationId);
        List<LocationDto> childrenDtoList = childrenList.stream().map(children -> {
            LocationDto childrenDto = new LocationDto();
            BeanUtils.copyProperties(children, childrenDto);

            //二级菜单
            String childrenLocationId = children.getLocationId();
            //三级
            List<Location> grandsonLocationList = locationMapper.getChildren(childrenLocationId);
            List<String> grandsonLocationIdList = grandsonLocationList.stream().map(Location::getLocationId).collect(Collectors.toList());
            List<LocationDto> grandsonLocationDtoList = grandsonLocationList.stream().map(grandsonLocation -> {
                LocationDto grandsonLocationDto = new LocationDto();
                BeanUtils.copyProperties(grandsonLocation,grandsonLocationDto);
                grandsonLocationDto.setChildren(null);
                grandsonLocationDto.setIdList(null);
                return grandsonLocationDto;
            }).collect(Collectors.toList());

            childrenDto.setChildren(grandsonLocationDtoList);
            childrenDto.setIdList(grandsonLocationIdList);
            return childrenDto;
        }).collect(Collectors.toList());
        List<String> parentIdList = childrenDtoList.stream().map(Location::getLocationId).collect(Collectors.toList());

        //荣康煤矿Dto
        LocationDto parentLocationDto = new LocationDto();
        BeanUtils.copyProperties(parentLocation,parentLocationDto);
        parentLocationDto.setChildren(childrenDtoList);
        parentLocationDto.setIdList(parentIdList);

        return parentLocationDto;
    }
}
